<template>
  <div class='container'>
    <el-dialog
      :title="title"
      :visible="dialogVisible"
      width="28%"
      @close="closeDialog">
      <el-form :model="subForm" :rules="rules" ref="subRef" label-width="80px">
          <el-form-item prop="subjectID" v-if="isShow" label="所属学科">
               <el-select style="width:300px" v-model="subForm.subjectID" size="small">
                  <el-option v-for="item in subjectList" :key="item.value" :label="item.label" :value="item.value"></el-option>
               </el-select>
          </el-form-item>
          <el-form-item label="目录名称" prop="directoryName">
              <el-input v-model="subForm.directoryName" style="width:300px" size="small"></el-input>
          </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="closeDialog">取 消</el-button>
    <el-button type="primary" @click="clickSubmit">确 定</el-button>
  </span>
</el-dialog>
  </div>
</template>

<script>
import { add, update } from '@/api/hmmm/directorys.js'
import { simple } from '@/api/hmmm/subjects.js'
export default {
  name: 'DirectorysAdd',
  data () {
    return {
      subjectList: [], // 学科列表
      // 表单数据
      subForm: {
        subjectID: null,
        directoryName: ''
      },
      rules: {
        subjectID: [{ required: true, message: '必选', trigger: ['blur', 'change'] }],
        directoryName: [{ required: true, message: '必填', trigger: ['blur', 'change'] }]
      },
      id: null, // 父组件直接控制
      title: ''
    }
  },
  props: {
    // 对话框显示与隐藏
    dialogVisible: {
      type: Boolean,
      default: false
    },
    // 输入框显示与隐藏
    isShow: {
      type: Boolean,
      default: true
    }
  },
  created () {
    this.getSubjects()
  },
  methods: {
    // 关闭对话框
    closeDialog () {
      this.$emit('update:dialogVisible', false)
      // 清空表单
      this.$refs.subRef.resetFields()
      // 将表单初始化
      this.subForm = {
        subjectID: null,
        directoryName: ''
      }
    },
    // 获取下拉列表数据
    async getSubjects () {
      const { data } = await simple()
      console.log(data)
      this.subjectList = data
    },
    // 提交
    clickSubmit () {
      this.$refs.subRef.validate(async boo => {
        if (boo) {
          // 判断是否时修改模式
          if (this.subForm.id) {
            // console.log(this.id)
            const res = await update({ ...this.subForm, id: +this.id })
            // console.log(res)
            if (res.status === 200) {
              this.$message.success('修改目录成功')
            }
          } else {
            console.log('添加')
            // 发送添加请求
            const res = await add(this.subForm)
            // console.log(res)
            if (res.status === 200) {
              // 提示
              this.$message.success('添加目录成功')
            }
          }
          // 通知父组件渲染页面
          this.$emit('render')
          // 关闭对话框
          this.closeDialog()
        }
      })
    }
  }
}
</script>

<style scoped lang='less'></style>
